<template>
  <div class="app-container">
      医院设置列表
      <!-- 条件查询 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
            <el-input  v-model="hospSetObj.hosname" placeholder="医院名称"/>
        </el-form-item>
        <el-form-item>
            <el-input v-model="hospSetObj.hoscode" placeholder="医院编号"/>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getHospSetList()">查询</el-button>
      </el-form>

      <div>
        <el-button type="danger" icon="el-icon-delete" @click="batchRemoveHospSet()">批量删除</el-button>
      </div>
      <br/>

      
      <el-table :data="list" stripe style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column type="index" width="50"/>
        <el-table-column prop="hosname" label="医院名称"/>
        <el-table-column prop="hoscode" label="医院编号"/>
        <el-table-column prop="apiUrl" label="api基础路径" width="200"/>
        <el-table-column prop="contactsName" label="联系人姓名"/>
        <el-table-column prop="contactsPhone" label="联系人手机"/>
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
                    {{ scope.row.status === 1 ? '可用' : '不可用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" align="center">
          <template slot-scope="scope">
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeHospById(scope.row.id)">删除</el-button>
              <el-button v-if="scope.row.status==1" type="primary" size="mini" icon="el-icon-delete" @click="lockHostSet(scope.row.id,0)">锁定</el-button>
              <el-button v-if="scope.row.status==0" type="danger" size="mini" icon="el-icon-delete" @click="lockHostSet(scope.row.id,1)">取消锁定</el-button>
              <router-link :to="'/hospSet/edit/'+scope.row.id">
                <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
              </router-link>

          </template>
        </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          :page-size="limit"
          :current-page="page"
          :total="total"
          layout="total, prev, pager, next, jumper"
          style="padding: 30px 0; text-align: center;"
          @current-change="getHospSetList">
        </el-pagination>

        
  </div>
</template>

<script>

import hospset from "@/api/hospset"

export default {
  data(){
    return {
      current:1,
      limit:3,
      list:[],
      hospSetObj:{},
      total:0,
      page:1,
      selections:[]  //多选框对象
    }
  },
  created() {
    this.getHospSetList()
  },
  methods: {
    //批量被选中时，触发控制器，获取整行的医院设置对象
    handleSelectionChange(selection){
      this.selections = selection
    },
    //查询页面，带分页
    getHospSetList(page=1){
      this.current = page
      hospset.getHospList(this.current,this.limit,this.hospSetObj)
          .then( //请求成功
            response => {   //response是接口返回数据
              this.list = response.data.records
              this.total = response.data.total
            }
          )
          .catch( //请求失败
            error =>{
              console.log(error);
            }
          )
    },
    //根据id删除医院设置
    removeHospById(id){
      this.$confirm('此操作将永久删除医院设置信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //删除医院设置信息
          hospset.removeHospSetByid(id)
          .then(
            response =>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新页面
              this.getHospSetList(1)
            }
          )
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    
    //批量删除
    batchRemoveHospSet(){
      //删除医院设置信息
      this.$confirm('此操作将永久删除医院设置信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //获取idList
          var idList = []
          for (let index = 0; index < this.selections.length; index++) {
            let id = this.selections[index].id;
            idList.push(id)
          }
          console.log(idList);
          //根据idList删除批量
          hospset.batchDeleteHospSetByIdList(idList)
          .then(
            response =>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新页面
              this.getHospSetList(1)
            }
          )
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },

    //状态锁定与解锁
    lockHostSet(id,status){
        hospset.lockHospitalSet(id,status).then(
            response =>{
              //刷新页面
              this.getHospSetList(1)
            }
        ).catch(
            error =>{
              console.log(error);
            }
        )
    }

    
    
  },
}
</script>